home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-03-22 | 14.3 KB | 224 lines | [TEXT/????] |
- /* Copyright 1994 Ralph Gonzalez */
-
- /*
- * FILE: xgraph.c
- * AUTHOR: R. Gonzalez, partially adapted from code by Don Snow
- * CREATED: April 16, 1993
- *
- * xgraph.c contains several X Window routines to supplement
- * a typical stdio-type application with a graphics window.
- * Your program must #include "xgraph.h" and must call
- * init_graphics() first.
- *
- * The device coordinate system (usually given in pixels) is norm-
- * alized to -1 to 1 horizontally by default. The vertical limits
- * depend on the screen's aspec *****************
- * Transform view coordinate y to window coordinates
- ************************************************************************/
- static int transform_y(double y)
- {
- return (int) (gGraphY+(y-gViewY)*gGraphHeight/gViewHeight);
- }
-
- /************************************************************************
- * pen_color() sets the current drawing color.
- ************************************************************************/
- void pen_color(color x)
- {
- XSetForeground(gDisplay,gGC,gPixels[x]);
- }
-
- /************************************************************************
- * background_color() sets the background drawing color.
- ************************************************************************/
- void background_color(color x)
- {
- XSetWindowBackground(gDisplay,gGraphWindow,gPixels[x]);
- }
-
- /************************************************************************
- * bring graphics window to front.
- ************************************************************************/
- void graphics_to_front(void)
- {
- XRaiseWindow(gDisplay,gGraphWindow);
- }
-
- /************************************************************************
- * erase_graphics() makes the graphics window the background color.
- ************************************************************************/
- void erase_graphics(void)
- {
- XClearWindow(gDisplay,gGraphWindow);
- XFlush(gDisplay);
- }
-
- /************************************************************************
- * draw_line() is used to draw lines using view coordinates.
- ************************************************************************/
- void draw_line(double x1,double y1,double x2,double y2)
- {
- int window_x1,
- window_y1,
- window_x2,
- window_y2;
-
- window_x1 = transform_x(x1);
- window_y1 = transform_y(y1);
- window_x2 = transform_x(x2);
- window_y2 = transform_y(y2);
-
- XDrawLine(gDisplay,gGraphWindow,gGC,window_x1,window_y1,
- window_x2,window_y2);
- XFlush(gDisplay);
-
- gPenX = window_x2;
- gPenY = window_y2;
- }
-
- /************************************************************************
- * Move present pen position to new position using view
- * coordinates. Nothing is drawn.
- ************************************************************************/
- void move_to(double x,double y)
- {
- gPenX = transform_x(x);
- gPenY = transform_y(y);
- }
-
- /************************************************************************
- * Draw from present pen position to new position using view
- * coordinates.
- ************************************************************************/
- void draw_to(double x,double y)
- {
- int window_x,
- window_y;
-
- window_x = transform_x(x);
- window_y = transform_y(y);
-
- XDrawLine(gDisplay,gGraphWindow,gGC,gPenX,gPenY,window_y,window_y);
- XFlush(gDisplay);
- gPenX = window_x;
- gPenY = window_y;
- }
-
- /************************************************************************
- * draw_circle() draws a circle using view coordinates.
- ************************************************************************/
- void draw_circle(double center_x,double center_y,double r)
- {
- int window_x,
- window_y,
- window_r,
- start_angle = 0,
- path_angle = 360*64; /* in 1/64 of degrees */
- unsigned int width,
- height;
-
- window_x = transform_x(center_x);
- window_y = transform_y(center_y);
- window_r = (int) (r*gGraphWidth/gViewWidth);
- window_x = window_x - window_r;
- window_y = window_y - window_r;
- width = window_r*2;
- height = width;
-
- XDrawArc(gDisplay,gGraphWindow,gGC,window_x,window_y,width,height,
- start_angle,path_angle);
- XFlush(gDisplay);
- gPenX = window_x;
- gPenY = window_y;
- }
-
- /************************************************************************
- * fill_circle() draws a circle using view coordinates. The circle
- * is filled with the present pen color
- ************************************************************************/
- void fill_circle(double center_x,double center_y,double r)
- {
- int window_x,
- window_y,
- window_r,
- start_angle = 0,
- path_angle = 360*64; /* in 1/64 of degrees */
- unsigned int width,
- height;
-
- window_x = transform_x(center_x);
- window_y = transform_y(center_y);
- window_r = (int) (r*gGraphWidth/gViewWidth);
- window_x = window_x - window_r;
- window_y = window_y - window_r;
- width = window_r*2;
- height = width;
-
- XFillArc(gDisplay,gGraphWindow,gGC,window_x,window_y,width,height,
- start_angle,path_angle);
- XFlush(gDisplay);
- gPenX = window_x;
- gPenY = window_y;
- }
-
- /************************************************************************
- * mouse_button_is_down() checks whether the mouse button is down,
- * returns TRUE or FALSE.
- ************************************************************************/
- boolean mouse_button_is_down(void)
- {
- XEvent theEventDummy;
-
- if (XPending(gDisplay))
- {
- XNextEvent(gDisplay,&theEventDummy);
- return TRUE;
- }
- else
- return FALSE;
- }
-
- /************************************************************************
- * wait until button is pressed
- ************************************************************************/
- void wait(void)
- {
- XEvent theEventDummy;
-
- XNextEvent(gDisplay,&theEventDummy);
- }
-
- /************************************************************************
- * get_mouse_location returns the mouse coordinates in terms of the
- * view coordinate system. It uses the inverse transformation of that
- * in transform_x() and transform_y().
- ************************************************************************/
- void get_mouse_location(double *x_ptr,double *y_ptr)
- {
- static Window dummy_root,dummy_child; /* static so we don't have
- to allocate them repeatedly */
- boolean in_screen;
- int root_x,
- root_y,
- x,
- y;
- unsigned int button_state;
-
- in_screen = XQueryPointer(gDisplay,gGraphWindow,&dummy_root,
- &dummy_child,&root_x,&root_y,&x,&y,&button_state);
-
- if (in_screen)
- {
- *x_ptr = gViewX+(x-gGraphX)*gViewWidth/gGraphWidth;
- *y_ptr = gViewY+(y-gGraphY)*gViewHeight/gGraphHeight;
- }
- else
- {
- *x_ptr = 0.;
- *y_ptr = 0.;
- }
-
- return;
- }
-
-